#!/usr/bin/env bash

###
#
#  bin: /opt/bin/etcd
#
###

cp ./etcd/etcd.conf /etc

# working dir
mkdir -p /var/lib/etcd/

# ETCD_ARGS：对应systemd unit文件中ExecStart后的变量；
# --cert-file，--key-file：分别指定etcd的公钥证书和私钥；
# --peer-cert-file，--peer-key-file：分别指定etcd的peers通信的公钥证书和私钥；
# --trusted-ca-file：指定客户端的CA证书；
# --peer-trusted-ca-file：指定peers的CA证书；
# --initial-cluster-state=new：表示这是新初始化集群，--name指定的参数值必须在--initial-cluster中

# 配置etcd的systemd unit文件

cat > /lib/systemd/system/etcd.service <<EOF
[Unit]
Description=Etcd Server
Documentation=https://github.com/coreos/etcd
After=network.target


[Service]
# User=root
#Type=simple
Type=notify
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd.conf
ExecStart=/bin/bash -c "GOMAXPROCS=$(nproc) /opt/bin/etcd --name=\"\${ETCD_NAME}\" --data-dir=\"\${ETCD_DATA_DIR}\" --listen-client-urls=\"\${ETCD_LISTEN_CLIENT_URLS}\" --listen-peer-urls=\"\${ETCD_LISTEN_PEER_URLS}\" --advertise-client-urls=\"\${ETCD_ADVERTISE_CLIENT_URLS}\" --initial-advertise-peer-urls=\"\${ETCD_INITIAL_ADVERTISE_PEER_URLS}\" --initial-cluster=\"\${ETCD_INITIAL_CLUSTER}\" --initial-cluster-state=\"\${ETCD_INITIAL_CLUSTER_STATE}\""

Restart=on-failure
RestartSec=10s
LimitNOFILE=40000

[Install]
WantedBy=multi-user.target
EOF


